aboutsummaryrefslogtreecommitdiff
path: root/pages/post/[id].tsx
blob: f1575c20dd231d29901a38b9c0c8598a27871ca8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { readdirSync, readFileSync } from 'fs';
import { join } from 'path';
import { ReactNode } from 'react';
import ReactMarkdown from 'react-markdown';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import rehypeRaw from 'rehype-raw';
import gfm from 'remark-gfm';

import Chapters, { chapter } from '../../components/chapters';
import Image from '../../components/image';
import Navbar from '../../components/navbar';
import Seperator from '../../components/seperator';
import Tags from '../../components/tag';

export interface ArticleMeta {
	title?: string;
	subtitle?: string;
	author?: string;
	tags?: Array<string>;
	date?: string;
	chapters?: Array<chapter>;
	cover?: string;
	id?: string;
}

var headingLevel = (input: string) => input?.match(/^[#]+/)[0]?.length || 0;

var sectionID = (input: string) =>
	input
		.replace(/[()\[\]{}!@#$%^&*<>?,./\;':"\\|=+]/g, '')
		.replace(/\s/g, '-')
		.toLowerCase();

function Heading(props: {
	children?: ReactNode;
	level?: number;
}) {
	var HeadingTag = 'h' + props.level as keyof JSX.IntrinsicElements;
	return <HeadingTag id={sectionID(props.children[0])} children={props.children} />;
}

function Code(props: {
	className?: string;
	children?: ReactNode;
}) {
	var language = /language-(\w+)/.exec(props.className || '');
	if (!language) return <code children={props.children} className={props.className} />;
	return <SyntaxHighlighter
		language={language[1]}
		children={props.children.toString().trim()}
		useInlineStyles={false}
		PreTag='div'
		style={{}}
	/>;
}

export function RenderedArticle(props: { content: string; }) {
	return <ReactMarkdown
		rehypePlugins={[rehypeRaw]}
		remarkPlugins={[gfm]}
		children={props.content}
		components={{
			img: ({ node, ...props }) => <Image src={props.src as string} alt={props.alt as string} />,
			hr: Seperator,

			h1: Heading, // TODO: fix this garbage
			h2: Heading,
			h3: Heading,
			h4: Heading,
			h5: Heading,
			h6: Heading,

			code: Code,
		}}
	/>;
}

export default function Post(props: {
	content: string;
	meta: ArticleMeta;
}) {
	return <div>
		<div className='centeredPage'>
			<div className='titleWrapper'>
				<h1>{props.meta.title}</h1>
				<p className='subtile'>{props.meta.subtitle}</p>
				{props.meta.tags && <Tags tags={props.meta.tags} />}
			</div>
			<div className='navAreaWrapper'>
				<div className='sticky'>
					<Navbar />
					<Chapters chapters={props.meta.chapters} />
				</div>
			</div>
			<div className='contentWrapper'>
				<RenderedArticle content={props.content} />
			</div>
		</div>
	</div>;
}

var parseTag = {
	'title': (val: string) => val,
	'subtitle': (val: string) => val,
	'author': (val: string) => val,
	'cover': (val: string) => val,
	'tags': (val: string) => val.split(',').map(i => i.trim()),
	'date': (val: string) => new Date(val).toDateString(),
};

function parseMeta(file: Array<string>): ArticleMeta {
	var meta: ArticleMeta = {};

	file.forEach(line => {
		if (!line.startsWith('[meta]: ')) return;
		var tags = line.match(/\[meta\]:\s+\<(.+?)\>\s+\((.+?)\)/);
		if (!tags || !tags[1] || !tags[2]) return;
		if (!parseTag.hasOwnProperty(tags[1])) return;
		meta[tags[1]] = parseTag[tags[1]](tags[2]);
	});

	return meta;
}

function parseToCRecursive(headings: Array<string>): Array<chapter> {
	interface WIPchapter extends chapter {
		unparsedChildren?: Array<string>;
	}
	var children: Array<WIPchapter> = [];

	var lowestLevel = headingLevel(headings[0]);
	var currentChildIndex = -1;
	for (var i in headings) {
		var localLevel = headingLevel(headings[i]);
		if (localLevel == lowestLevel) {
			var chapterName = headings[i].match(/^[#]+\s+(.+)/)[1];
			children.push({
				name: chapterName,
				sectionLink: '#' + sectionID(chapterName),
				unparsedChildren: [],
			});
			currentChildIndex += 1;
		} else {
			children[currentChildIndex].unparsedChildren.push(headings[i]);
		}
	}

	children.map(child => {
		child.children = parseToCRecursive(child.unparsedChildren);
		delete child.unparsedChildren;

		return child;
	});

	return children as Array<chapter>;
}

function parseToC(file: Array<string>): Array<chapter> {
	var fileAsStr = file.join('\n');
	fileAsStr = fileAsStr.replace(/```.*?```/gs, ''); // filter out code blocks from table of contents
	var fileAsArr = fileAsStr.split('\n');
	var chapterStrings = fileAsArr.filter(line => line.startsWith('#'));
	return parseToCRecursive(chapterStrings);
}

function preprocessor(fileContent: string) {
	var fileAsArr = fileContent.split('\n');
	var meta = parseMeta(fileAsArr);
	meta.chapters = parseToC(fileAsArr);
	var result = fileAsArr.join('\n').trim();
	return { meta, result };
}

export function getStaticProps(props: { params: { id: string; }; }) {
	var filename = join('posts/', props.params.id + '.md');
	var filecontent = readFileSync(filename).toString().trim();

	var parsed = preprocessor(filecontent);
	parsed.meta.id = props.params.id;

	return {
		props: {
			content: parsed.result,
			meta: parsed.meta,
		},
	};
}

export function getStaticPaths() {
	var files = readdirSync('posts').filter(f => f.endsWith('.md'));

	return {
		paths: files.map((f) => {
			return {
				params: {
					id: f.substr(0, f.length - 3),
				},
			};
		}),
		fallback: false,
	};
}